'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate',
        'validate.en' ], function($) {
    return {
        init : function() {
            this.initEdit();
            this.initCreate();
            this.initDelete();
            this.resetPassword();
        },

        initEdit : function() {

            $(document).on('shown.bs.modal', '#edit', function(e) {

            	$('select[name="authority"]').change(function() {
                    var role = $(this).val()
                    if(role == "ROLE_DEPARTMENT_USER") {
                        $('.department').removeClass('hide')
                    } else {
                        $('.department').addClass('hide')
                    }
                })
            	
                $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate

                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });

        },        
        initDelete : function() {

            $(document).on('shown.bs.modal', '#delete', function(e) {

            	$('select[name="authority"]').change(function() {
                    var role = $(this).val()
                    if(role == "ROLE_DEPARTMENT_USER") {
                        $('.department').removeClass('hide')
                    } else {
                        $('.department').addClass('hide')
                    }
                })
            	
                $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate

                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });

        },
        initCreate : function() {

            $(document).on('shown.bs.modal', '#create', function(e) {
                
                $('select[name="authority"]').change(function() {
                    var role = $(this).val()
                    if(role == "ROLE_DEPARTMENT_USER") {
                        $('.department').removeClass('hide')
                    } else {
                        $('.department').addClass('hide')
                    }
                })
                
                $('input[name="email"]').on('blur', function(e){
                  var val = $(this).val();
                  var surfix = val.split("@")[0];
                  $('input[name="name"]').val(surfix);
                });
                
                
                $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate

                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return  $.bt_validate.result[$.bt_validate.form_id];
                        
                     }
                })
            });
        },
        resetPassword: function() {
            $(document).on("shown.bs.modal", "#reset", function (e) {
              
            	$(document).find('form[data-validate="true"]').bt_validate()  //bind validate
    
                var $form = $(document).find("form")                          //bind shiftform
                
                $form.shiftform({
                    beforeSubmit: function (el) {
                        
                        var password = $('input[name="password"]').val().trim();
                        var confirmPassword = $('input[name="confirmPassword"]').val().trim();
                        
                        if (!(password === confirmPassword)) {
                          var html = '<div class="tooltip fade bottom in" role="tooltip" id="confirm-password" style="display: block; top: 30px; left: 89.8281px;"><div class="tooltip-arrow" style="left: 50%; border-bottom-color: rgb(235, 204, 209);"></div><div class="tooltip-inner" style="background-color: rgb(242, 222, 222); padding: 10px 12px; max-width: 500px; color: rgb(185, 74, 72);"><i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;两次密码不一致！<div id="tlt_1500621070305" <="" div=""></div></div></div>';
                          $(".role-confirm-password").append(html);
                          setTimeout("$('#confirm-password').remove();", 1000);
                          return false;
                        }
                      
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                        
                    }
                })
    
            });
          }
    }
})
